$(function () {
    $(".head").load("head.html")
    $(".top").load("gw.html")
    $.post("news/gouwuche",{},function (data) {
        console.log(data)
        var prices=0;
        $.each(data.list,function (i,news) {
            if (i%2==0){
                $(".car_tab").find('tbody').append(
                    "            <tr class=\"car_tr\">\n"+
                    "                <td>\n" +
                    "                    <div class=\"c_s_img\"><img src='../../files/"+news.filename+"' width=\"73\" height=\"73\" /></div>\n" +
                    "                   "+news.name+"\n" +
                    "                </td>\n" +
                    "                <td align=\"center\">\n" +
                    "                    <div>"+news.num+"</div>\n" +
                    "                </td>\n" +
                    "                <td align=\"center\" style=\"color:#ff4e00;\">￥"+news.price*news.num+"</td>\n" +
                    "            </tr>\n"
                )
            }
            else {
                $(".car_tab").find('tbody').append(
                    "            <tr>\n"+
                    "                <td>\n" +
                    "                    <div class=\"c_s_img\"><img src='../../files/"+news.filename+"' width=\"73\" height=\"73\" /></div>\n" +
                    "                   "+news.name+"\n" +
                    "                </td>\n" +
                    "                <td align=\"center\">\n" +
                    "                    <div>"+news.num+"</div>\n" +
                    "                </td>\n" +
                    "                <td align=\"center\" style=\"color:#ff4e00;\">￥"+news.price*news.num+"</td>\n" +
                    "            </tr>\n"
                )
            }
            prices+=news.price*news.num;
        });
        $(".two_t").append(
            "<input type='hidden' value='"+prices+"' class='prices'>"
        )
        $(".queren").append(
            " <tr height=\"70\">\n" +
            "                <td align=\"right\">\n" +
            "                \t<b style=\"font-size:14px;\">应付款金额：<span style=\"font-size:22px; color:#ff4e00;\">￥"+prices+"</span></b>\n" +
            "                </td>\n" +
            "              </tr>\n" +
            "              <tr height=\"70\">\n" +
            "                <td align=\"right\"><a href=\"javascript:;\"><img  class='fukuan' src=\"images/btn_sure.gif\" /></a></td>\n" +
            "              </tr>"
        );


    })
    $.post("news/dizhi",{},function (data) {
        $(".two_t").append(
            "<input type='hidden' value='"+data.id+"' class='userid'>"
        )
        console.log(data)
        $(".peo_tab").append(
            " <tr>\n" +
            "                <td class=\"p_td\" width=\"160\">用户名</td>\n" +
            "                <td width=\"395\">"+data.username+"</td>\n" +
            "              </tr>\n" +
            "              <tr>\n" +
            "                <td class=\"p_td\" width=\"160\">电子邮件</td>\n" +
            "                <td width=\"395\">"+data.email+"</td>\n" +
            "              </tr>\n" +
            "              <tr>\n" +
            "                <td class=\"p_td\">手机</td>\n" +
            "                <td>"+data.mobile+"</td>\n" +
            "              </tr>\n"+
            "              <tr>\n" +
            "                  <td class=\"p_td\">新增地址<label><input value='lala' type=\"radio\" name=\"dz\"></label></td>\n" +
            "                  <td><input style='width: 100%' class='lalalala' type='text'>备注：<input class='beizhu' type='text'></td>\n" +
            "              </tr>"
        )
        $.each(data.list,function (i,news) {
            console.log(news)
            if(news.isdefault==1){
                $(".peo_tab").append(
                    "              <tr>\n" +
                    "                  <td class=\"p_td\">地址<label><input value='"+news.id+"' checked='checked' type=\"radio\" name=\"dz\"></label></td>\n" +
                    "                  <td><input style='width: 100%' class='"+news.id+"lala' readonly value='"+news.address+"'></td>\n" +
                    "              </tr>"
                )
            }else {
                $(".peo_tab").append(
                    "              <tr>\n" +
                    "                  <td class=\"p_td\">地址<label><input value='"+news.id+"' type=\"radio\" name=\"dz\"></label></td>\n" +
                    "                  <td><input style='width: 100%' class='"+news.id+"lala' readonly value='"+news.address+"'></td>\n" +
                    "              </tr>"
                )
            }
        })
    })

    $(".queren").on('click','.fukuan',function () {
        var idz=$("."+$("input[name='dz']:checked").val()+"lala").val()
        if(idz==null){
            alert("请选择地址，再确认订单")
        }else {
            if ($("input[name='dz']:checked").val()=="lala"){
                $.post("news/addDZ",{"address":idz,"remark":$(".beizhu").val(),"userid":$(".userid").val()})
            }
            // $.post("news/addOrder",{"address":idz,"prices":$(".prices").val()},function (data) {
            //     console.log(data)
            //     localStorage.idz=idz;
            //     localStorage.prices=$(".prices").val();
            //     if (data==true){
            //        location.href="http://localhost:7777/BuyCar_Three.html"
            //     }
            // })
            localStorage.idz=idz;
            localStorage.prices=$(".prices").val();
            $.post("news/aliapy",{"prices":$(".prices").val(),"idz":escape(idz)},function(msg){
                console.log(msg)
                let divForm = document.getElementsByTagName('divform')
                if (divForm.length) {
                    document.body.removeChild(divForm[0])
                }
                const div=document.createElement('divform');
                div.innerHTML=msg;//这个msg就是后台返回的一串from表单
                document.body.appendChild(div);
                document.forms[0].setAttribute('target', '_blank') // 新开窗口跳转
                // document.forms[0].submit();
                $("[name='punchout_form']").submit()
            })

            // location.href="http://localhost:7777/BuyCar_Three.html"
        }
    })
    // //拿地址后面的参数
    function getData(){
        if(document.URL.indexOf("?")<0) return;//获取当前url地址,如果没参数.直接返回
        var str=document.URL.split("?")[1];
        var obj={};
        var arr;
        //当只有一个参数传过来时
        if(str.indexOf("&")<0){
            if(str.indexOf("=")<0) return;
            arr=str.split("=");
            obj[arr[0]]=arr[1];
            return obj;
        }
        //当有多个参数时
        arr=str.split("&");
        var arr1;
        for(var i=0;i<arr.length;i++){
            arr1=arr[i].split("=");
            obj[arr1[0]]=arr1[1];
        }
        return obj;
    }
    var obj =getData();
    console.log(obj)

    if(obj.lala!=""){
        localStorage.idz=unescape(obj.idz);
        localStorage.prices=obj.prices;
        localStorage.out=obj.out;
        location.href="http://localhost:7777/BuyCar_Three.html"
    }
})